LoginSignup
0
0

More than 5 years have passed since last update.

Tips & tricks (日々更新)

Last updated at Posted at 2016-12-28

Tip 1.1 : Select2コントロールの内容を再度初期化せずに更新する

//ajaxを実行して新内容を取得する
success:function (result, textStatus, xhr) {
   if(result.success){
      $items = result.items;
      //元のコンテンツをクリアする
      $('#opt-select2').html('').select2({data: [{id: null, text: null}]});
      var data = [];
      jQuery.each($items, function(key, value){
         var item_obj = value;
         data.push({id:item.id, text: item.text});
      });
      $("#opt-select2").html('').select2({data: $(data)});
   }else{
      ...
   }

Tip 1.2 : Select2のPlaceholderをjQueryで設定する

//ajaxを実行して新内容を取得する
success:function (result, textStatus, xhr) {
   if(result.success){
      $items = result.items;
      //元のコンテンツをクリアする
      $('#opt-select2').html('').select2({data: [{id: null, text: null}]});
      var data = [id:'', text:'']; //Placeholderを表示するように用意する
      jQuery.each($items, function(key, value){
         var item_obj = value;
         data.push({id:item.id, text: item.text});
      });
      $("#opt-select2").html('').select2({data: $(data)});
      $("#opt-select2").select2({placeholder: 'New Place holder', allowClear:true});
   }else{
      ...
   }

Tip 2: Bootstrap wysihtml5-editorの内容を設定する

var iframe = $('#wysihtml5-editor-div').find('iframe');
//書かれていた内容を削除する
$(iframe).contents().find('.wysihtml5-editor').html('');
//新内容を記入する
$('#wysihtml5-editor-div').data("wysihtml5").editor.setValue($new_content);

Tip3 : AdminLTEに含むBootstrap wysihtml5-editorライブラリのバグ

Tip2で参照されていたeditorはいつもnullである場合は使っているライブラリを確認してください。一応最新版のAdminLTEに含むライブラリだとこの既存バグは存在されています。修正法は以下になります。

bootstrap-wysihtml5.js
 createEditor: function(options) {
            options = options || {};

            // Add the toolbar to a clone of the options object so multiple instances
            // of the WYISYWG don't break because "toolbar" is already defined
            options = $.extend(true, {}, options);
            options.toolbar = this.toolbar[0];

            var editor = new wysi.Editor(this.el[0], options);

            if(options && options.events) {
                for(var eventName in options.events) {
                    editor.on(eventName, options.events[eventName]);
                }
            }
            return editor; //<ーこのラインを追加する
        },

あるいは修正した版をダウンロードして切り替える
bootstrap-wysihtml5

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0