Search code examples
twitter-bootstrapobjectbootstrap-table

bootstrap-table column-event adding a event object


Currently i am using window.operateEvents as data-events for one of my bootstrap-table columns Thing is i want to split this up in multiple files for permission perposes. Then only what needs to be loaded will be loaded.

This works now but i am combining two objects properties. property by property. Is this the fastest/best way?

Also is there a way to make this work without even combining the two objects that bootstrap-table will recognize thats better?

/**
 * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
 * @param obj1
 * @param obj2
 * @returns newObj a new object based on obj1 and obj2
 */
function merge_options(obj1,obj2){
    // If object1 doesn't exist just give back obj2
    if(typeof(obj1) === "undefined" ){
        return obj2;
    }

    var newObj = {};
    for (var attrname in obj1) { newObj [attrname] = obj1[attrname]; }
    for (var attrname in obj2) { newObj [attrname] = obj2[attrname]; }
    return newObj;
}


    /**
     * @brief operator events for actions on the rows
     * @type void
     */
    window.operateEvents = merge_options(window.operateEvents,{
        'click .edit': function(e,value,row,index){
            requestConfirm(e,value,row,"update", 
                function(){
                    $('#table').bootstrapTable('refresh', "{silent: true}");
                }
            );
            e.stopPropagation();
        }                          
    });

    /** Add delete functionality */
    window.operateEvents = merge_options(window.operateEvents, {
         'click .remove': function(e,value,row,index){
            requestConfirm(e,value,row,"delete", 
                function(){
                        $('#table').bootstrapTable('refresh', "{silent: true}");
                }
            );
            e.stopPropagation();
        }       
    });

Solution

  • The best way to do this is to start adding each action to your operateEvents object by property name.

    For example:

    window.operateEvents['click .edit'] = (function() {
    //some edit stuff
    });
    
    window.operateEvents['click .remove'] = (function() {
    //remove some stuff
    });
    

    Hope this helps.